package com.hyperchain.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.swing.filechooser.FileSystemView;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterOutputStream;

/**
 * 二维码工具
 * @Author:debug (SteadyJack)
 * @Link: weixin-> debug0868  qq-> 1948831260
 * @Date: 2020/11/16 22:38
 **/
@Slf4j
public class QRCodeUtil {

    //CODE_WIDTH：二维码宽度，单位像素
    private static final int CODE_WIDTH = 400;
    //CODE_HEIGHT：二维码高度，单位像素
    private static final int CODE_HEIGHT = 400;
    //FRONT_COLOR：二维码前景色，0x000000 表示黑色
    private static final int FRONT_COLOR = 0x000000;
    //BACKGROUND_COLOR：二维码背景色，0xFFFFFF 表示白色
    //演示用 16 进制表示，和前端页面 CSS 的取色是一样的，注意前后景颜色应该对比明显，如常见的黑白
    private static final int BACKGROUND_COLOR = 0xFFFFFF;
 
    public static File createCodeToFile(String content, File codeImgFileSaveDir, String fileName) {
        File codeImgFile = null;
        try {
            if (StrUtil.isBlank(content) || StrUtil.isBlank(fileName)) {
                return codeImgFile;
            }
            content = content.trim();
            if (codeImgFileSaveDir==null || codeImgFileSaveDir.isFile()) {
                //二维码图片存在目录为空，默认放在桌面...
                codeImgFileSaveDir = FileSystemView.getFileSystemView().getHomeDirectory();
            }
            if (!codeImgFileSaveDir.exists()) {
                //二维码图片存在目录不存在，开始创建...
                codeImgFileSaveDir.mkdirs();
            }
 
            //核心代码-生成二维码
            BufferedImage bufferedImage = getBufferedImage(content);

            codeImgFile = new File(codeImgFileSaveDir, fileName);
            ImageIO.write(bufferedImage, "png", codeImgFile);
            log.info("「二维码初始化」:结果「成功」,文件路径「{}」",codeImgFile.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return codeImgFile;
    }
 
    /**
     * 生成二维码并输出到输出流, 通常用于输出到网页上进行显示，输出到网页与输出到磁盘上的文件中，区别在于最后一句 ImageIO.write
     * write(RenderedImage im,String formatName,File output)：写到文件中
     * write(RenderedImage im,String formatName,OutputStream output)：输出到输出流中
     * @param content  ：二维码内容
     * @param outputStream ：输出流，比如 HttpServletResponse 的 getOutputStream
     */
    public static void createCodeToOutputStream(String content, OutputStream outputStream) {
        try {
            if (StrUtil.isBlank(content)) {
                return;
            }
            content = content.trim();
            //核心代码-生成二维码
            BufferedImage bufferedImage = getBufferedImage(content);
 
            //区别就是这一句，输出到输出流中，如果第三个参数是 File，则输出到文件中
            ImageIO.write(bufferedImage, "png", outputStream);
 
            log.info("二维码图片生成到输出流成功...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //核心代码-生成二维码
    private static BufferedImage getBufferedImage(String content) throws WriterException {
 
        //com.google.zxing.EncodeHintType：编码提示类型,枚举类型
        Map<EncodeHintType, Object> hints = new HashMap();
 
        //EncodeHintType.CHARACTER_SET：设置字符编码类型
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
 
        //EncodeHintType.ERROR_CORRECTION：设置误差校正
        //ErrorCorrectionLevel：误差校正等级，L = ~7% correction、M = ~15% correction、Q = ~25% correction、H = ~30% correction
        //不设置时，默认为 L 等级，等级不一样，生成的图案不同，但扫描的结果是一样的
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
 
        //EncodeHintType.MARGIN：设置二维码边距，单位像素，值越小，二维码距离四周越近
        hints.put(EncodeHintType.MARGIN, 1);
        
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_HEIGHT, hints);
        BufferedImage bufferedImage = new BufferedImage(CODE_WIDTH, CODE_HEIGHT, BufferedImage.TYPE_INT_BGR);
        for (int x = 0; x < CODE_WIDTH; x++) {
            for (int y = 0; y < CODE_HEIGHT; y++) {
                bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? FRONT_COLOR : BACKGROUND_COLOR);
            }
        }
        return bufferedImage;
    }

    /**
     * 压缩文本
     * @param text
     * @return
     */
    public static String zipBase64(String text) {
        //创建一个新的字节数组输出流。缓冲区容量最初为 32 字节
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            //使用默认压缩器和缓冲区大小创建新的输出流
            try (DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out)) {
                deflaterOutputStream.write(text.getBytes(Charset.forName("UTF-8")));
            }
            return Base64.getEncoder().encodeToString(out.toByteArray());
        } catch (IOException e) {
        }
        return "";
    }

    /**
     * 解压文本
     * @param text
     * @return
     */
    public static String unzipBase64(String text) {
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            //使用默认解压缩器和缓冲区创建新的输出流
            try (OutputStream outputStream = new InflaterOutputStream(os)) {
                outputStream.write(Base64.getDecoder().decode(text.getBytes()));
            }
            return new String(os.toByteArray(), Charset.forName("UTF-8"));
        } catch (IOException e) {

        }
        return "";
    }

    /**
     * 解析上传二维码内容(直解析utf-8和GBK编码的二维码)
     * @param QRImg 二维码图片内容
     * @throws IOException
     * @throws NotFoundException
     */
    public static String analysis(File QRImg) throws IOException, NotFoundException {
        BufferedImage image = ImageIO.read(QRImg);
        BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        HashMap map = new HashMap();
        map.put(DecodeHintType.CHARACTER_SET, "utf-8");
        Result result = new MultiFormatReader().decode(bb, map);
        String QRContents=result.getText();
        if (result.getText().contains("�")){
            //乱码了，更换编码方式
            System.out.println("乱码了，更换编码格式(GBK)");
            map.put(DecodeHintType.CHARACTER_SET, "GBK");
            result = new MultiFormatReader().decode(bb, map);
            QRContents=result.getText();
        }
        System.out.println("二维码文本内容："+QRContents);
        return QRContents;
    }
    /**
     * 解析本地二维码内容(直解析utf-8和GBK编码的二维码)
     * @param filePath 二维码图片地址
     * @throws IOException
     * @throws NotFoundException
     */
    public static String analysis(String filePath) throws IOException, NotFoundException {
        BufferedImage image = ImageIO.read(new File(filePath));
        BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        HashMap map = new HashMap();
        map.put(DecodeHintType.CHARACTER_SET, "utf-8");
        Result result = new MultiFormatReader().decode(bb, map);
        String QRContents=result.getText();
        if (result.getText().contains("�")){
            //乱码了，更换编码方式
            System.out.println("乱码了，更换编码格式(GBK)");
            map.put(DecodeHintType.CHARACTER_SET, "GBK");
            result = new MultiFormatReader().decode(bb, map);
            QRContents=result.getText();
        }
        System.out.println("二维码文本内容："+QRContents);
        return QRContents;
    }
    /**
     * 解析本地二维码内容,自定义编码格式
     * @param filePath 二维码图片地址
     * @param character 二维码内容编码格式
     * @throws IOException
     * @throws NotFoundException
     */
    public static String analysis(String filePath,String character) throws IOException, NotFoundException {
        BufferedImage image = ImageIO.read(new File(filePath));
        BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        HashMap map = new HashMap();
        map.put(DecodeHintType.CHARACTER_SET,character);
        map.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        //复杂模式
        map.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        Result result = new MultiFormatReader().decode(bb, map);
        String QRContents=result.getText();
        return QRContents;
    }
    /**
     * 解析二维码,此方法解析一个路径的二维码图片（带logo）
     * path:二维码图片路径
     */
    public static String deEncodeByPath(String path) {
        String content = null;
        BufferedImage image;
        try {
            image = ImageIO.read(new File(path));
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "GBK");
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
            Result result = new MultiFormatReader().decode(binaryBitmap, hints);//解码
            System.out.println("图片中内容：  ");
            System.out.println("content： " + result.getText());
            content = result.getText();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            //这里判断如果识别不了带LOGO的图片，重新添加上一个属性
            try {
                image = ImageIO.read(new File(path));
                LuminanceSource source = new BufferedImageLuminanceSource(image);
                Binarizer binarizer = new HybridBinarizer(source);
                BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
                Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
                //设置编码格式
                hints.put(DecodeHintType.CHARACTER_SET, "GBK");
                //设置优化精度
                hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
                //设置复杂模式开启（我使用这种方式就可以识别微信的二维码了）
                hints.put(DecodeHintType.PURE_BARCODE,Boolean.TYPE);
                Result result = new MultiFormatReader().decode(binaryBitmap, hints);//解码
                System.out.println("图片中内容：  ");
                System.out.println("content： " + result.getText());
                content = result.getText();
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (NotFoundException e2) {
                e2.printStackTrace();
            }
        }
        return content;
    }

}
